package com.itheima.test;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 实现数据的导入
 *
 * @author mewp
 * @date 2022/11/22 09:46
 **/
public class POIDemo03 {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws IOException, ParseException {
        FileInputStream is = new FileInputStream("D:\\mewp\\资料\\Java报表数据可视化教程\\day01\\资料\\用户导入测试数据.xlsx");
        //有内容的workBook工作簿
        Workbook workbook = new XSSFWorkbook(is);
        //获取第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        //当前工作表的最后一行的索引值
        int lastRowIndex = sheet.getLastRowNum();
        Row row = null;

        for (int i = 1; i <= lastRowIndex; i++) {
            row = sheet.getRow(i);
            //用户名
            String userName = row.getCell(0).getStringCellValue();
            //手机号
            String phone = null;
            try {
                phone = row.getCell(1).getStringCellValue();
            } catch (Exception e) {
                phone = row.getCell(1).getNumericCellValue() + "";
            }
            //省份
            String province = row.getCell(2).getStringCellValue();
            //城市
            String city = row.getCell(3).getStringCellValue();
            //工资
            Integer salary = ((Double) row.getCell(4).getNumericCellValue()).intValue();
            //入职日期
            Date hireDate = sdf.parse(row.getCell(5).getStringCellValue());
            //出生日期
            Date birthday = sdf.parse(row.getCell(6).getStringCellValue());
            //现住地址
            String address = row.getCell(7).getStringCellValue();

            System.out.println(userName + "  " + phone + "  " + province + "  " + city + "  " + salary + "  " + hireDate + "  " + birthday + "  " + address);

        }

    }
}
